import {
  getChallengeInProgress,
  getChallengeEnded,
  launchChallenge,
  signForChallenge,
  getChallengeInProgressUser,
  getChallengeEndedUser,
  getChallengeInfo,
  realTimeRanking, compete
} from '@/api/challenge'

const challenge = {
  actions: {
    getChallengeInProgress ({ commit }) {
      return new Promise((resolve, reject) => {
        getChallengeInProgress().then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    getChallengeEnded ({ commit }) {
      return new Promise((resolve, reject) => {
        getChallengeEnded().then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    getChallengeInProgressUser ({ commit }, userId) {
      return new Promise((resolve, reject) => {
        getChallengeInProgressUser(userId).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    getChallengeEndedUser ({ commit }, userId) {
      return new Promise((resolve, reject) => {
        getChallengeEndedUser(userId).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    launchChallenge ({ commit }, createInfo) {
      const challengeName = createInfo.challengeName.trim()
      return new Promise((resolve, reject) => {
        launchChallenge(challengeName, createInfo.startTime, createInfo.endTime, createInfo.stockCode, createInfo.startingFunds, createInfo.maxParticipantsNumber, createInfo.introduction).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    SignForChallenge ({ commit }, signInfo) {
      const challengeId = signInfo.challengeId
      return new Promise((resolve, reject) => {
        signForChallenge(challengeId, signInfo.userId, signInfo.conditionSheetId).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetChallengeInfo ({ commit }, challengeId) {
      return new Promise((resolve, reject) => {
        getChallengeInfo(challengeId).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    RealTimeRanking ({ commit }, challengeId) {
      return new Promise((resolve, reject) => {
        realTimeRanking(challengeId).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    Compete ({ commit }, competeInfo) {
      const challengeId = competeInfo.challengeId
      return new Promise((resolve, reject) => {
        compete(challengeId, competeInfo.userId).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default challenge
